<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoResource.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/avatar.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/resource.php';

class AvatarDao {
	private $_db;
	private $_resourceDao;
	
	public function __construct() {
		$this->_db = new DbConnection();
		$this->_db->connect();
		$this->_resourceDao = new ResourceDao();
	}

	public function addAvatar($avatar) {
		$query = sprintf("CALL sp_AddAvatar('%s', '%s')",$avatar->getUserId(), $avatar->getResourceId());		
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . mysql_error();
		    return 0;
		}		
		
		return 1;
	}
	
	//get resource given avatar
	//returns resource model if successfully
	//otherwise return null
	public function getResource($userID) {
		global $ubi;
		$query = sprintf("SELECT * FROM avatars WHERE UserId = '%d' ORDER BY DefaultAvatar DESC", $userID);
		
		$result = $this->_db->executeQuery($query);
		
		if (mysql_num_rows($result)) {
			$row = mysql_fetch_assoc($result);
			$resourceId = $row['ResourceId'];
			
			return $this->_resourceDao->getResource($resourceId);						
		} else {
			$resource = new ResourceModel();
			$resource->setResourceName($ubi['USER']['DEFAULT_AVATAR_RESOURCE']);
			
			return $resource;
		}
		
		if ($result) {
			mysql_free_result($result);
		}
	}
	
	public function getResources($userID) {
		$query = "SELECT * FROM avatars WHERE UserId = '" . $userID . "'";
		
		$result = $this->_db->executeQuery($query);
		
		$resources = array();
		while ($row = mysql_fetch_assoc($result)) {			
			$resourceId = $row['ResourceId'];
			$resources[] = $resourceId;			
		}
		
		if ($result) {
			mysql_free_result($result);
		}
		
		return $resources;
	}
	
	public function getAvatars($userID) {
		$query = "SELECT * FROM avatars WHERE UserId = '" . $userID . "'";
		
		$result = $this->_db->executeQuery($query);
		
		$avatars = array();
		while ($row = mysql_fetch_assoc($result)) {			
			$avatar = new AvatarModel();
			$avatar->setDefault($row['DefaultAvatar']);
			$avatar->setResourceID($row['ResourceId']);
			$avatar->setUserID($row['UserId']);
			$avatars[] = $avatar;
		}
		
		if ($result) {
			mysql_free_result($result);
		}
		
		return $avatars;
	}
	
	public function setDefaultAvatar($userID, $resourceID) {
		$query = sprintf("CALL sp_UpdateDefaultAvatar('%d', '%d')", $userID, $resourceID);
		$result = $this->_db->executeQuery($query);
		if (!$result) {
			echo 'Could not run udpate token due to ' . mysql_error();
			return 0;
		}
		
		return 1;
	}
	
	public function deleteAvatar($resourceID) {
		$query = sprintf("CALL sp_DeleteAvatar('%d')", $resourceID);
		$result = $this->_db->executeQuery($query);
		if (!$result) {
			echo 'Could not run udpate token due to ' . mysql_error();
			return 0;
		}
		
		return 1;
	}
}
?>